package com.example.easypoi.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.example.easypoi.mapper.ExcelPoiMapper;
import com.example.easypoi.model.Citytable;
import com.example.easypoi.model.ExcelPoi;
import com.example.easypoi.service.ExcelPoiService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: 朱超
 * @createDate: 2022/1/20
 * @version: 1.0
 * @remark：备注
 */
@Slf4j
@Service
public class ExcelPoiServiceImpl implements ExcelPoiService {
    @Autowired(required = false)
    private ExcelPoiMapper excelPoiMapper;
    @Autowired(required = false)
    private HttpServletResponse response;

    @Autowired(required = false)
    private IExcelVerifyHandler excelVerifyHandler;

    /**
     * 导出文件
     *
     * @return
     */
    @Override
    public List<ExcelPoi> ExcelPoiList() {
//        List<ExcelPoi> excelPois = excelPoiMapper.ExcelPoiList();
        return excelPoiMapper.ExcelPoiList();
    }

    @Override
    public List<ExcelPoi> ExcelPoiListNumber() {
        List<ExcelPoi> excelPoi = null;
        List<ExcelPoi> excelPois = excelPoiMapper.ExcelPoiList();
        for (ExcelPoi pois : excelPois) {
            excelPoi = excelPoiMapper.ExcelPoiListNumber(pois.getExcelName());
        }
        return excelPoi;
    }


    @Override
    public Map<String, Object> addUser(MultipartFile file) throws Exception {
        Map<String, Object> map = new HashMap<>();
        //导入的基本配置
        ImportParams params = new ImportParams();
        //表头一行
        params.setHeadRows(1);
        //标题一行
        params.setTitleRows(1);
        //代表导入这里是需要验证的（根据字段上的注解）
//        params.setNeedVerify(true);
        //设及一个自定义校验 （自定义校验名字不可重复）
        params.setVerifyHandler(excelVerifyHandler);
        //使用框架自身导入工具
        ExcelImportResult<Citytable> result = ExcelImportUtil.importExcelMore(file.getInputStream(), Citytable.class, params);
        //导入成功的数据
        List<Citytable> list = result.getList();
        //失败结果集
        List<Citytable> failList = result.getFailList();

        //拿到导出失败的工作簿
        Workbook failWorkbook = result.getFailWorkbook();
        failWorkbook.write(response.getOutputStream());
        //验证是否有失败的数据
        try {
            if (result.isVerifyFail()) {
                ServletOutputStream fos = response.getOutputStream();
//                //mime类型
//                response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//                response.setHeader("Content-disposition", "attachment;filename=error.xlsx");
//                result.getFailWorkbook().write(fos);
//                fos.close();

                map.put("ER", failList);
                map.put("ee", "重复数据");
                map.get("ER");
                map.get("ee");
                return map;
            }
        } catch (Exception e) {
            map.put("ER", "解析文件失败");
            map.get("ER");
            return map;

        }
        excelPoiMapper.insert(list);
        map.put("ER", list);
        map.get("ER");
        map.put("ee", "添加成功");
        return map;
//        return CommonResult.success(list, "添加成功");


    }

    @Override
    public Integer insert(List<Citytable> excels) throws Exception {
        try {
            return excelPoiMapper.insert(excels);
        }catch (Exception e){
            throw new Exception("导入数据重复");
        }

    }

}
